#include "../Maths/Constants.h"
#include "../Maths/MinMax.h"
#include "../Maths/Projections.h"
#include "FlyCamera.h"


FlyCamera::FlyCamera(Vector3Arg position, Vector3Arg rotation)
    : transform(Matrix44::kIdentity)
    , position(Vector3::kZero)
    , rotation(Vector3::kZero)
{
    UpdateTransform(position, rotation);
}


void FlyCamera::UpdateTransform(Vector3Arg deltaTranslation, Vector3Arg deltaRotation)
{
    position += MulVectorTransform(deltaTranslation, transform);
    rotation += deltaRotation;
    rotation.x = Max(Min(rotation.x, kHalfPi-0.001f), 0.001f-kHalfPi);

    float pitch = rotation.x;
    float yaw = rotation.y;
    Vector3 forward(
        sin(yaw) * cos(pitch),
        sin(pitch),
        -cos(yaw) * cos(pitch));
    Vector3 up(0, 1, 0); // TODO
    transform = TransformInverseRT(LookAt(position, position + forward, up));
}


const Matrix44& FlyCamera::GetTransform() const
{
    return transform;
}
